package com.example.jucsample.cdl;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

public class DragonBall {

    public static void main(String[] args) throws Exception {
        // 一个或多个线程等待一组线程完成后执行

        CountDownLatch latch = new CountDownLatch(7);
        for (int i = 0; i < 7; i++) {
            final int n = i + 1;
            new Thread(() -> {
                Random random = new Random();
                int cost = random.nextInt(5);
                try {
                    Thread.sleep(cost * 1000);
                    System.out.println(String.format("找到%s星球", n));
                    latch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        latch.await();
        System.out.println("出来吧神龙");
    }

}
